package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.entity.UserRole;
import cn.jdy.authority.service.UserRoleService;
import cn.jdy.authority.service.UserService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.WatchUser;
import cn.jdy.plugins.service.AdvertisingService;
import cn.jdy.plugins.service.LabelService;
import cn.jdy.plugins.service.LineService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.service.RelatedVideoService;
/**
 * @title 提现控制类
 * @author lutan
 */
@Controller
@RequestMapping("/watchUser")
public class WatchUserCtrl extends BaseCtrl {
	@Autowired
	private LineService lineService;
	@Autowired
	private RelatedVideoService relatedVideoService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private UserService userService;
	@Autowired
	private WatchUserService watchUserService;
	@Autowired
	private ConfService confService;
	@Autowired
	private LabelService labelService;
	@Autowired
	private AdvertisingService advertisingService;
	@Autowired
	protected HttpSession session;
	// 默认列表路径
	private static String viewName = "redirect:/WatchUser/list?s=";

	/**
	 * 初始化
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required = false) Integer s) {
		ModelAndView mv = new ModelAndView();
		if (s != null)
			switch (s) {
			case 1:
				mv.addObject("msg", "保存成功！");
				break;
			case 2:
				mv.addObject("msg", "更新成功！");
				break;
			case 3:
				mv.addObject("msg", "删除成功！");
				break;
			case 4:
				mv.addObject("msg", "操作失败！");
				break;
			default:
				break;
			}
		mv.setViewName("/operate/watchUserList.jsp");
		return mv;
	}

	/**
	 * 异步查询
	 * 
	 * @param WatchUser
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(WatchUser watchUser,String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {

			jsonMap.put("dataList", watchUserService.query(watchUser));
			jsonMap.put("iTotalDisplayRecords", watchUserService.count(watchUser,fromDate,toDate));
			jsonMap.put("iTotalRecords", watchUserService.count());
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/watchUserDetail.jsp");
		User user = (User) session.getAttribute("user");
		List<UserRole> list = userRoleService.find("userid", user.getId());
		String roleid = "";
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getRoleid()
					.equals("4028b3044de1a396014de1ba2b2e0000")
					|| list.get(i).getRoleid()
							.equals("ff8080814db89f0a014db8d169290000")) {
				roleid = "0";
			}
		}
		if (roleid != "0") {
			
			mv.addObject("zlineid",
					userService.findOne("id", user.getId())
							.getZlineid());
			mv.addObject("linedata", lineService.find("organid", userService
					.findOne("username", user.getUsername()).getOrganid()));
		} else {
			
			mv.addObject("linedata", lineService.findAll());
		}
		mv.addObject("data",
				id == null || id == 0 ? id : this.watchUserService.find(id));
		return mv;
	}

	/**
	 * 保存（更新）直播
	 * 
	 * @param WatchUser
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("/save")
	public ModelAndView save(WatchUser WatchUser) {
		ModelAndView mv = new ModelAndView();
		User user = (User) session.getAttribute("user");
		try {
		
			
			List<UserRole> list = userRoleService.find("userid", user.getId());
			String roleid = "";
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getRoleid()
						.equals("4028b3044de1a396014de1ba2b2e0000")
						|| list.get(i).getRoleid()
								.equals("ff8080814db89f0a014db8d169290000")) {
					roleid = "0";
				}
			}
			if (roleid != "0") {
			
			}
			if (null == null) {
				WatchUser.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			
				this.watchUserService.save(WatchUser);
				mv.setViewName(this.viewName.concat("1"));
			} else {
				WatchUser.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			
				this.watchUserService.update(WatchUser);
				mv.setViewName(this.viewName.concat("2"));
				// 清理内容页缓存
				NginxPurgeCacheUtil.purge(6, WatchUser.getId());
			}
			// 清理内容页缓存
			NginxPurgeCacheUtil.purge(5, WatchUser.getId());
		} catch (Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "直播保存异常", e);
		}
		return mv;
	}

	/**
	 * 删除直播
	 * 
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			advertisingService.delete("WatchUserId", id);
			relatedVideoService.delete("WatchUserId", id);
			this.watchUserService.delete(id);

			mv.setViewName(this.viewName.concat("3"));
			// 清理内容页缓存
			NginxPurgeCacheUtil.purge(6, id);
		} catch (Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除直播异常！", e);
		}
		return mv;
	}

	/**
	 * 状态切换
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping("/statusSwitch")
	@ResponseBody
	public Map<String, Object> statusSwitch(Long id, String status) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();		
		try {
			
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "状态切换异常", e);
		}
		return jsonMap;
	}
	

}
